package com.cssw.fyzb.core.mao.task;

import com.cssw.fyzb.core.model.mos.DailyTaskMo;
import com.cssw.fyzb.core.mongo.MongoClient;
import com.cssw.fyzb.core.util.DocUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.bson.Document;

/**
 * @description:
 * @author: zzf
 * @time: 2021/1/22 15:15
 */
@Component
public class DailyTaskMaoImpl implements DailyTaskMao {

    @Autowired
    private MongoClient mongoClient;

    @Override
    public DailyTaskMo findByUserId(Long userId) {
        Document doc = mongoClient.getCollection("daily_task")
                .find(new Document("userId", userId))
                .first();
        return DocUtil.decoce(doc, DailyTaskMo.class);
    }

    @Override
    public void save(DailyTaskMo dailyTaskMo) {
        mongoClient.getCollection("daily_task")
                .insertOne(DocUtil.encode(dailyTaskMo));
    }

    @Override
    public void update(DailyTaskMo dailyTaskMo) {
        Document query = new Document();
        query.put("userId", dailyTaskMo.getUserId());

        mongoClient.getCollection("daily_task").updateOne(query, new Document("$set",
                DocUtil.encode(dailyTaskMo)));
    }
}
